package com.leo.exception;

import com.leo.result.Result;
import com.leo.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 030
 * @date 2020/9/3 17:32
 * @description 全局异常处理
 * @className GlobalExceptionHandler
 */
@Slf4j
@ControllerAdvice   // ControllerAdvice 注解会 自动捕获 @RequestMapping 注解的方法中     的异常的
public class GlobalExceptionHandler {

	@ExceptionHandler(Exception.class)
	@ResponseBody
	public Result error(Exception e){

//		e.printStackTrace();
		log.error(e.getMessage());
		return Result.error();
	}


	@ExceptionHandler(ArithmeticException.class)
	@ResponseBody
	public Result error(ArithmeticException e){

//		e.printStackTrace();
		log.error(e.getMessage());
		return Result.error().code(ResultCode.ARITHMETIC_EXCEPTION.getCode())
				.message(ResultCode.ARITHMETIC_EXCEPTION.getMessage());
	}

	/**
	 * 处理业务异常
	 * @param e
	 * @return
	 */
	@ExceptionHandler(BusinessException.class)
	@ResponseBody
	public Result error(BusinessException e){

//		e.printStackTrace();
		log.error(e.getErrMsg());
		return Result.error().code(e.getCode())
				.message(e.getErrMsg());
	}

}
